﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CashResource;
using CashCommon;

namespace Cash.DialogFroms
{
    public partial class FormParamsOfJournal : Form
    {
        public FormParamsOfJournal(DateTime dtFrom, DateTime dtTo)
        {
            InitializeComponent();
            this.Text = CommonText.ParamOfJournalFormTitle;
            this._dtFrom = dtFrom;
            this._dtTo = dtTo;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                // Проверка полей
                if (dtpFrom.Value == null)
                    throw new ECashUsers(MessageType.UserMessage, String.Format(CommonError.FieldIsEmpty, lblWith.Text), "");

                if (dtpTo.Value == null)
                    throw new ECashUsers(MessageType.UserMessage, String.Format(CommonError.FieldIsEmpty, lblTo.Text), "");

                if (OnResultOk != null)
                    OnResultOk(dtpFrom.Value, dtpTo.Value);

                this.DialogResult = DialogResult.OK;
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        #region Fields
        
        private DateTime _dtFrom = DateTime.Today;
        private DateTime _dtTo = DateTime.Today;

        #endregion

        #region Events

        public event DelegateDataFromDataTo OnResultOk;
        
        #endregion

        private void FormParamsOfJournal_Load(object sender, EventArgs e)
        {
            dtpFrom.Value = this._dtFrom;
            dtpTo.Value = this._dtTo;
        }

        private void dtpFromTo_ValueChanged(object sender, EventArgs e)
        {
            DateTimePicker dt = sender as DateTimePicker;
            if (dt != null && dt.Name == "dtpFrom")
                dtpTo.Value = dt.Value;

            if (dt != null && dt.Name == "dtpTo")
            { 
                if (dt.Value < dtpFrom.Value)
                    dt.Value = dtpFrom.Value;
            }
        }

    }
}
